package com.netposa.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
@ConfigurationProperties(
   prefix = "netposa.oracle"
)
@MapperScan(
   basePackages = {"com.netposa.mapper.oracle"},
   sqlSessionFactoryRef = "sqlSessionFactoryOracle"
)
public class OracleDbConfig {
   private static final Logger log = LoggerFactory.getLogger(OracleDbConfig.class);
   private String url;
   private String driverClassName;
   private String username;
   private String password;

   @Bean(
      name = {"oracleDataSource"}
   )
   public DataSource dataSourceOracle() {
      log.info("url :" + this.url + "; driverClassName :" + this.driverClassName + "; username: " + this.username + "; password: " + this.password);
      return DataSourceBuilder.create().driverClassName(this.driverClassName).url(this.url).username(this.username).password(this.password).build();
   }

   @Bean
   public SqlSessionFactory sqlSessionFactoryOracle(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {
      SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
      factoryBean.setDataSource(oracleDataSource);
      ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
      factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/oracle/*.xml"));
      return factoryBean.getObject();
   }

   @Bean
   public SqlSessionTemplate sqlSessionTemplateOracle(@Qualifier("sqlSessionFactoryOracle") SqlSessionFactory sqlSessionFactoryOracle) throws Exception {
      SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryOracle);
      return template;
   }

   public String getUrl() {
      return this.url;
   }

   public String getDriverClassName() {
      return this.driverClassName;
   }

   public String getUsername() {
      return this.username;
   }

   public String getPassword() {
      return this.password;
   }

   public void setUrl(String url) {
      this.url = url;
   }

   public void setDriverClassName(String driverClassName) {
      this.driverClassName = driverClassName;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof OracleDbConfig)) {
         return false;
      } else {
         OracleDbConfig other = (OracleDbConfig)o;
         if (!other.canEqual(this)) {
            return false;
         } else {
            label59: {
               Object this$url = this.getUrl();
               Object other$url = other.getUrl();
               if (this$url == null) {
                  if (other$url == null) {
                     break label59;
                  }
               } else if (this$url.equals(other$url)) {
                  break label59;
               }

               return false;
            }

            Object this$driverClassName = this.getDriverClassName();
            Object other$driverClassName = other.getDriverClassName();
            if (this$driverClassName == null) {
               if (other$driverClassName != null) {
                  return false;
               }
            } else if (!this$driverClassName.equals(other$driverClassName)) {
               return false;
            }

            Object this$username = this.getUsername();
            Object other$username = other.getUsername();
            if (this$username == null) {
               if (other$username != null) {
                  return false;
               }
            } else if (!this$username.equals(other$username)) {
               return false;
            }

            Object this$password = this.getPassword();
            Object other$password = other.getPassword();
            if (this$password == null) {
               if (other$password != null) {
                  return false;
               }
            } else if (!this$password.equals(other$password)) {
               return false;
            }

            return true;
         }
      }
   }

   protected boolean canEqual(Object other) {
      return other instanceof OracleDbConfig;
   }

   public int hashCode() {
      
      int result = 1;
      Object $url = this.getUrl();
      result = result * 59 + ($url == null ? 43 : $url.hashCode());
      Object $driverClassName = this.getDriverClassName();
      result = result * 59 + ($driverClassName == null ? 43 : $driverClassName.hashCode());
      Object $username = this.getUsername();
      result = result * 59 + ($username == null ? 43 : $username.hashCode());
      Object $password = this.getPassword();
      result = result * 59 + ($password == null ? 43 : $password.hashCode());
      return result;
   }

   public String toString() {
      return "OracleDbConfig(url=" + this.getUrl() + ", driverClassName=" + this.getDriverClassName() + ", username=" + this.getUsername() + ", password=" + this.getPassword() + ")";
   }
}
